package com.cheyun.netsalev3.adapter

import androidx.lifecycle.ViewModel
import com.cheyun.netsalev3.bean.AZItemEntity

/**
 * 带标题栏Adapter 使用AZItemEntity数据类型
 */
class TitleAdapter<T>(override var layoutId: Int,
                      override var viewModel: ViewModel?,
                      override var callback: ItemCallback<AZItemEntity<T>>? = ItemCallback())
    : DefaultAdapter<AZItemEntity<T>>(layoutId, viewModel, callback) {


    var temlist: List<AZItemEntity<T>>? = null

    fun getSortLetters(position: Int): String? {
        return if (temlist == null) {
            null
        } else {
            var item = temlist!![position]
            return item.sortLetters
        }

    }

    fun getSortLettersFirstPosition(letters: String): Int {
        if (temlist == null) {
            return -1
        }
        var position = -1
        for (index in temlist!!.indices) {
            if (temlist!![index].sortLetters.equals(letters)) {
                position = index
                break
            }
        }
        return position
    }

    fun getNextSortLetterPosition(position: Int): Int {
        if (temlist == null || temlist!!.isEmpty() || temlist!!.size <= position + 1) {
            return -1
        }
        var resultPosition = -1
        for (index in position + 1 until temlist!!.size) {
            if (!temlist!![position].sortLetters.equals(temlist!![index].sortLetters)) {
                resultPosition = index
                break
            }
        }
        return resultPosition
    }

}
